package com.shengdou.as.slopsapptest.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.shengdou.as.slopsapptest.R;
import com.shengdou.as.slopsapptest.utils.LogUtil;

/**
 * Created by AS on 2016/12/5.
 * 水池状态图
 */

public class MyImageView extends ImageView {
    protected int corners;
    public static final int CORNER_TOP_LEFT = 1;
    public static final int CORNER_TOP_RIGHT = 1 << 1;
    public static final int CORNER_BOTTOM_LEFT = 1 << 2;
    public static final int CORNER_BOTTOM_RIGHT = 1 << 3;
    public static final int CORNER_ALL = CORNER_TOP_LEFT | CORNER_TOP_RIGHT | CORNER_BOTTOM_LEFT | CORNER_BOTTOM_RIGHT;
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.water_cheek);
    float w = bitmap.getWidth();
    float h = bitmap.getHeight();
    float polHeight;
    public int alarms;
    Paint paint = new Paint();//绘制颜色的画笔
    Paint decapaint = new Paint();//绘制刻度的画笔
    private float space;//间隔
    private float[] YLabel;//设置数字显示
    /**
     * 内存中创建的Canvas
     */
    private Canvas mCanvas;
    private Canvas mBackCanvas;//beijing
    /**
     * mCanvas绘制内容在其上
     */
    private Bitmap mBitmap;
    private Bitmap mBitmapBack;

    public MyImageView(Context context) {
        super(context, null);
    }

    public MyImageView(Context context, AttributeSet attrs) {
        super(context, attrs, 0);
    }

    public MyImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.corners = CORNER_ALL;
    }


    public void setWind(float poolHeight, int alarm) {
        this.polHeight = poolHeight;
        this.alarms = alarm;
        this.postInvalidate();  //可以子线程 更新视图的方法调用。
        invalidate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        YLabel = new float[9];
        for (int i = 0; i < YLabel.length; i++) {
            YLabel[i] = (float) (i * 0.5);
        }
//        LogUtil.e("WINDTH+++", w + "------gaodu +" + h);
        // 初始化bitmap
        mBitmap = Bitmap.createBitmap((int) w, (int) h, Bitmap.Config.ARGB_8888);
        mBitmapBack = Bitmap.createBitmap((int) w, (int) h, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
        mBackCanvas = new Canvas(mBitmapBack);

        // 绘制遮盖层
//         mCanvas.drawColor(Color.parseColor("#c0c0c0"));

        mBackCanvas.drawBitmap(BitmapFactory.decodeResource(getResources(),
                R.mipmap.water), null, new RectF(0, 0, (int) w, (int) h), null);
        mCanvas.drawBitmap(BitmapFactory.decodeResource(getResources(),
                R.mipmap.water_cheek), null, new RectF(0, 0, (int) w, (int) h), null);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(mBitmapBack, 0, 0, null);
        space = (h - 10) / 9;
        int radius = 19;
        if (polHeight > 0) {
            int cavarDataHeight = (int) (h - (polHeight / 0.5 * space));
            paint.setStyle(Paint.Style.FILL);
            paint.setAntiAlias(true); // 去锯齿
            if (alarms == 0) {
                paint.setColor(0xF087B44E);//绿色
            } else if (alarms == 1) {
                paint.setColor(0xF0E37F1E);//橙色
            } else if (alarms == 2) {
                paint.setColor(0xF0FF0000);//红色
            }
            RectF r1 = new RectF(0, cavarDataHeight, (int) w, (int) h);
            canvas.drawRoundRect(r1, radius, radius, paint);
            int notRoundedCorners = corners ^ CORNER_ALL;
            //哪个角不是圆角我再把你用矩形画出来
            if ((notRoundedCorners & CORNER_TOP_LEFT) != 0) {
                canvas.drawRect(0, cavarDataHeight, radius, cavarDataHeight + radius, paint);
            }
            if ((notRoundedCorners & CORNER_TOP_RIGHT) != 0) {
                canvas.drawRect(r1.right - radius, cavarDataHeight, r1.right, cavarDataHeight + radius, paint);
            }
        }
        cavarGraduation(canvas);//绘制刻度
        canvas.drawBitmap(mBitmap, 0, 0, null);
    }

    private void cavarGraduation(Canvas canvas) {

        decapaint.setStyle(Paint.Style.FILL);
        decapaint.setAntiAlias(true); // 去锯齿
        decapaint.setColor(Color.WHITE);
        decapaint.setStrokeWidth(1);
        decapaint.setTextSize(20);
        for (int i = 3; i * space < h - 10; i++) {
            canvas.drawLine(0, (h - space * i), 20, (h - space * i), decapaint);
            canvas.drawText(YLabel[i] + " m", 25, (h - space * i) + (space / 5), decapaint);
        }
    }
}
